package com.xuzimian.globaldemo.quartz;

import com.xuzimian.globaldemo.quartz.job.SimpleJob;
import com.xuzimian.globaldemo.quartz.job.UseSpringBeanJob;
import com.xuzimian.globaldemo.quartz.util.SchedulerHelper;
import com.xuzimian.globaldemo.quartz.util.SpringQuartzManager;
import org.junit.jupiter.api.Test;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.text.MessageFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;

public class SimpleDemo {

    @Test
    public void testStartQuartzTask() throws SchedulerException, IOException {

        SchedulerHelper.addJob("SimpleJob1",
                "SimpleJobs", SimpleJob.class, "0 0/1 * 15-24 12 ?");

        SchedulerHelper.addJob("SimpleJob2",
                "SimpleJobs", SimpleJob.class, "0 0/1 * * * ?");

        SchedulerHelper.startJobs();

        System.out.println("SimpleDemo start...");

        System.in.read();
    }

    @Test
    public void testStartUseSpringBeanJobQuartzTask() throws SchedulerException, IOException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-quartz.xml");

        SpringQuartzManager springQuartzManager= (SpringQuartzManager) ctx.getBean("springQuartzManager");
        springQuartzManager.addJob(UseSpringBeanJob.class,"UseSpringBeanJob",
                "UseSpringBeanJobs", "1 * * * * ?");



        System.out.println("UseSpringBeanJob start...");

        System.in.read();
    }



    @Test
    public void testStartNonAgriculturalTask() throws IOException, SchedulerException {
        String beforeAndAfterPublishDayCronTemplate = "0 0/30 * {0} {1} ?";
        String currentPublishDayCronTemplate = "0/10 * * {0} {1} ?";
        LocalDate publishDay = getPubishDay();


        //1.公布日期前三天前，定时当月该三天每天每半小时执行一次
        try {
            int beforeCurThreeDayOfMonth = publishDay.getDayOfMonth() > 3 ? publishDay.plusDays(-3).getDayOfMonth() : publishDay.plusDays(-publishDay.getDayOfMonth()+1).getDayOfMonth();

            String beforePublishDayCron = MessageFormat.format(beforeAndAfterPublishDayCronTemplate,
                    beforeCurThreeDayOfMonth + "-" + (publishDay.getDayOfMonth() - 1),
                    publishDay.getMonthValue());

            beforePublishDayCron="0 0/2 * 24-27 12 ?";
            SchedulerHelper.addJob("非农公布日前三天采集",
                    "非农", SimpleJob.class, beforePublishDayCron);
        } catch (SchedulerException e) {

        }

        //2.公布日当天为10秒一次
        try {
            String currentPublishDayCron = MessageFormat.format(currentPublishDayCronTemplate,
                    publishDay.getDayOfMonth(), publishDay.getMonthValue());

            currentPublishDayCron="0/10 * * 24 12 ?";
            SchedulerHelper.addJob("非农公布日当天",
                    "非农", SimpleJob.class, currentPublishDayCron);
        } catch (SchedulerException e) {

        }

        //3.公布日之后，如未收集到，为半小时一次,直到月尾最后一天。
        try {
            String afterPublishDayCron = MessageFormat.format(beforeAndAfterPublishDayCronTemplate,
                    publishDay.plusDays(1).getDayOfMonth() + "-" + publishDay.getMonth().maxLength(), publishDay.getMonthValue());
            afterPublishDayCron="0 0/3 * 24-31 12 ?";
            SchedulerHelper.addJob("非农公布日之后",
                    "非农", SimpleJob.class, afterPublishDayCron);
        } catch (SchedulerException e) {

        }
        SchedulerHelper.startJobs();

        System.in.read();
    }

    /***
     * 获取当前月的公布日期，每个月的第一个周五。
     * @return
     */
    private LocalDate getPubishDay() {
        LocalDate now = LocalDate.now();
        LocalDate publishDay = null;
        for (int i = 0; i < 8; i++) {
            publishDay = now.plusDays(i);
            if (DayOfWeek.FRIDAY.equals(publishDay.getDayOfWeek())) {
                break;
            }
        }
        return publishDay;
    }
}
